<?php

namespace Biz\service\bizActivity;

use Biz\model\MijiaSmartKitchen\MiJiaSmartKitchenUser;
use Biz\model\MijiaSmartKitchen\MiJiaSmartKitchenUserLottery;
use Biz\model\MijiaSmartKitchen\MijiaSmartKitchenWinningRecord;
use Biz\pockets\constInterface\MjiaActivityConfig;
use Doraemon\tools\Log;

class mjKitchenHome
{

    public static $platform = 0;
    public static $uuid = '';
    public static function auth($uid)
    {
        //用户登陆校验
        $openid = $_COOKIE['openid'] ?? '';
        $token = $_COOKIE['access_token'] ?? '';
        if (($uid == 0) && !\Biz\service\bizActivity\base\Util::isWechatUser($openid)) {
            throw new \Exception("未授权的用户");
        }
        self::$platform = $uid > 0 ? MjiaActivityConfig::ACTIVITY_PLATFORM_DEFAULT : MjiaActivityConfig::ACTIVITY_PLATFORM_WECHAT;
        self::$uuid = $uid > 0 ? $uid : $openid;
    }


    //获取抽奖次数
    public static function getLotteryTimes($uid)
    {
        self::auth($uid);

        $info = MiJiaSmartKitchenUser::getInfo(self::$platform, self::$uuid);

        return $info['times'] ?? 0;
    }

    //消耗抽奖次数
    public static function expendTime($uid)
    {
        self::auth($uid);

        $info = MiJiaSmartKitchenUser::getInfo(self::$platform, self::$uuid);

        if (empty($info)) throw new \Exception("未完成任务");
        if ($info['times'] < 1) throw new \Exception("抽奖次数已耗尽");

        return MiJiaSmartKitchenUser::expendTimes(self::$platform, self::$uuid, $info['v']);
    }

    //完成任务
    public static function task($uid)
    {
        self::auth($uid);

        $exists = MiJiaSmartKitchenUser::exists(self::$platform, self::$uuid);

        if (!$exists) MiJiaSmartKitchenUser::record(self::$platform, self::$uuid);
    }

    //用户基础信息
    public static function homeInfo($uid)
    {
        self::auth($uid);
        //1.用户信息（抽奖状态，地址填写状态）
        //2.任务完成
        $address = false;
        $prize = null;
        $task = MiJiaSmartKitchenUser::exists(self::$platform, self::$uuid);
        if ($task) {
            $lottery = MiJiaSmartKitchenUserLottery::hasUserLottery(self::$uuid, self::$platform);
            $lottery = $lottery ? 2 : 1;
            $info = MijiaSmartKitchenWinningRecord::getUserWinning(self::$uuid, self::$platform);
            if ($info) {
                $address = $info['address'] === '';
                $prize = [
                    'id' => $info['id'],
                    'prize_name' => $info['prize_name'],
                    'prize_win_img' => MjiaActivityConfig::WIN_IMAGES[$info['prize_id']] ?? '',
                ];
            }
        } else {
            $lottery = 0;
        }
        return [
            'lottery' => $lottery, //0 未完成任务 1 未抽奖 2 已抽奖
            'address_stat' => $address,
            'start_status' => date("Y-m-d H:i:s") > MjiaActivityConfig::ACTIVITY_START_TIME,
            'end_status' => date("Y-m-d H:i:s") > MjiaActivityConfig::ACTIVITY_END_TIME,
            'prize' => $prize,
        ];
    }

    //设置中奖信息地址
    public static function setDelivery($uid, $name, $phone, $address, $id)
    {
        self::auth($uid);

        if ($id == 0) {
            $info = MijiaSmartKitchenWinningRecord::getUserWinning(self::$uuid, self::$platform);
            $id = $info['id'] ?? 0;
        }

        if ($id > 0) MijiaSmartKitchenWinningRecord::setDelivery($id, $name, $phone, $address);
    }

}